package com.geekerp.authorization_server.service;

import com.geekerp.authorization_server.pojo.Oauth2BasicUser;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SmsUserService  {
//    @Resource
//    UserMapper userMapper;
//
//    @Resource
//    RolesMapper rolesMapper;

    @Resource
    PasswordEncoder passwordEncoder;

    private static final Logger logger = LoggerFactory.getLogger(SmsUserService.class);

    /**
     * 根据用户手机号查询用户详细信息
     * @param phoneNum 手机号
     * @return customUser
     * @throws UsernameNotFoundException
     *
     *
     * customUser是继承了User的类
     * UserDto是查询数据库的类
     */

    public UserDetails loadUserByPhoneNum(String phoneNum) throws UsernameNotFoundException {
        logger.info("用户登录认证, phoneNum={}", phoneNum);
        Oauth2BasicUser basicUser = new Oauth2BasicUser();
        basicUser.setId(1);
        basicUser.setName("云逸");
        basicUser.setAccount("admin");
        basicUser.setPassword("$2a$10$K7nVcC.75YZSZU1Fq6G6buYujG.dolGYGPboh7eQbtkdFmB0EfN5K");
        basicUser.setMobile("17683906001");
        basicUser.setEmail("17683906001@163.com");
        basicUser.setDeleted(false);
        if (basicUser == null) {
            throw new UsernameNotFoundException("账号不存在");
        }
        return basicUser;
    }

    /**
     * UserDTO转CustomUser对象
     * @param userDTO
     * @return user
     */
//    private CustomUser convertUserDTO2CustomUser(UserDTO userDTO) {
//        //查询用户的角色信息，并返回存入user中
//        List<Role> roles = rolesMapper.getRolesByUid(userDTO.getId());
//        // 权限大的角色排在前面
//        roles.sort(Comparator.comparing(Role::getId));
//        CustomUser user = new CustomUser(userDTO.getUsername(), userDTO.getPassword(),
//                userDTO.getEnabled()==1, true, true,
//                true, new ArrayList<>());
//        user.setId(userDTO.getId());
//        user.setNickname(userDTO.getNickname());
//        user.setPhoneNum(userDTO.getPhoneNum());
//        user.setEmail(userDTO.getEmail());
//        user.setUserface(userDTO.getUserface());
//        user.setRegTime(userDTO.getRegTime());
//        user.setUpdateTime(userDTO.getUpdateTime());
//        user.setRoles(roles);
//        user.setCurrentRole(roles.get(0));
//        return user;
//    }

}
